package com.zxp.server.controller;


import com.zxp.server.config.Result;
import com.zxp.server.dto.CommentInfo;
import com.zxp.server.service.IBlogCommentService;
import com.zxp.server.util.token.annotation.CheckToken;
import com.zxp.server.util.token.tokenService;
import com.zxp.server.vo.BlogComment;
import com.zxp.server.vo.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 朱小皮
 * @since 2022-06-11
 */
@Api(value = "博客评论管理", tags = "博客评论管理")
@RestController
@RequestMapping("/blogComment")
public class BlogCommentController {

    @Autowired
    private tokenService tokenService;
    @Autowired
    private IBlogCommentService blogCommentService;

    @ApiOperation(value = "获取博客评论")
    @GetMapping("/list")
    public Result getComment(@RequestParam String blogId) {
        List<CommentInfo> commentList = blogCommentService.getCommentList(blogId);
        return  Result.success("获取成功",commentList);
    }


    @ApiOperation(value = "提交评论")
    @PostMapping("/commit")
    @CheckToken
    public Result replyComment(@RequestBody BlogComment comment, HttpServletRequest request) {
        User user = tokenService.getUserByToken(request);
        if (user==null){
            Result error = Result.error("未登录或用户信息过期，请重新登录！");
            error.setState("unauthorize");
            return error;
        }
        comment.setUid(user.getId());
        blogCommentService.getBaseMapper().insert(comment);
        return  Result.success("评论成功");
    }


}
